/*
 * Copyright Radosław Smogura, 2012. All rights reserved.
 *
 * The content of this file is confidential and it is intelectual property
 * of copyright owners.
 * 
 *
 * $URL: http://reprap-ide.googlecode.com/svn/trunk/RepRapIDE/RepRapProject/src/main/java/eu/softper/reprap/generator/Layer.java $
 * $Id: Layer.java 3 2012-11-19 10:58:30Z mail@smogura.eu $
 * $Revision: 3 $
 */
package eu.softper.reprap.generator;

import java.math.BigDecimal;
import java.util.*;

/**
 *
 * @author Radosław Smogura
 */
public class Layer<T extends Number & Comparable<T>> {
    private BigDecimal zPos;
    
    private Set<SceneLine<T>> outlines;

    private Map<DecimalVertex<T>, Collection<SceneLine<T>>> linesFromVertex = new HashMap<DecimalVertex<T>, Collection<SceneLine<T>>>();
    
    private List<SceneLine<T>> infills = new ArrayList<SceneLine<T>>(0);
    
    private List<List<SceneLine<T>>> reducedOutlines = new ArrayList<List<SceneLine<T>>>(3);
    
    /** Ordinal number of layer. */
    private int number;
    
    protected void addMulitVal(DecimalVertex<T> v, SceneLine<T> l) {
        Collection<SceneLine<T>> c = linesFromVertex.get(v);
        if (c == null) {
            c = new ArrayList<SceneLine<T>>(1);
            linesFromVertex.put(v, c);
        }
        if (!c.contains(l))
            c.add(l);
    }
    
    public Layer(int number, BigDecimal zPos, Set<SceneLine<T>> outlines) {
        this.zPos = zPos;
        this.outlines = outlines;
        this.number = number;
        
        /* Process cache. */
        Iterator<SceneLine<T>> i = outlines.iterator();
        while (i.hasNext()) {
            SceneLine<T> l = i.next();
            addMulitVal(l.getV1(), l);
            addMulitVal(l.getV2(), l);
        }
    }

    public BigDecimal getzPos() {
        return zPos;
    }

    public Set<SceneLine<T>> getOutlines() {
        return outlines;
    }

    public List<SceneLine<T>> getInfills() {
        return infills;
    }

    public void setInfills(List<SceneLine<T>> infills) {
        this.infills = infills;
    }
    
    public static class ZPositionComparator implements Comparator<Layer> {
        public static final ZPositionComparator INSTANCE = new ZPositionComparator();
        
        @Override
        public int compare(Layer o1, Layer o2) {
            return o1.getzPos().compareTo(o2.getzPos());
        }
    }

    public Map<DecimalVertex<T>, Collection<SceneLine<T>>> getLinesFromVertex() {
        return linesFromVertex;
    }

    public List<List<SceneLine<T>>> getReducedOutlines() {
        return reducedOutlines;
    }

    public int getNumber() {
        return number;
    }
}
